//
//  HBCategoryHeadView.m
//  HeartBeat
//
//  Created by karong on 2023/9/18.
//

#import "HBCategoryHeadView.h"
#import "HBCategoryModel.h"
#import "HBChannelSortCell.h"

@interface HBCategoryHeadView ()
@property (weak, nonatomic) IBOutlet UICollectionView *listView;
@property (nonatomic, strong) HBCategoryModel * categoryModel;
@property (nonatomic, assign) NSInteger selectIndex;
@end

@implementation HBCategoryHeadView
- (void)setDataWithObject:(id)object {
    self.categoryModel = object;
    [self.listView reloadData];
}

#pragma mark - UICollectionViewDelete
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.categoryModel.sort_type.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HBChannelSortCell * channelSortCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HBChannelSortCell" forIndexPath:indexPath];
    [channelSortCell setDataWithObject:self.categoryModel.sort_type[indexPath.row] select:self.selectIndex == indexPath.row];
    return channelSortCell;
}


#pragma mark - UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGSize size = CGSizeZero;
    HBCategoryItemModel * categoryItemModel = self.categoryModel.sort_type[indexPath.row];
    CGFloat width = [categoryItemModel.title boundingRectWithSize:CGSizeMake(MAXFLOAT, 0.0) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:(self.selectIndex == indexPath.row)?[UIFont systemFontOfSize:14 weight:(UIFontWeightMedium)]:[UIFont systemFontOfSize:12 weight:(UIFontWeightRegular)]} context:nil].size.width;
    size.width = width;
    size.height = 45;
    return size;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    UIEdgeInsets inset = UIEdgeInsetsZero;
    inset.left = 16;
    inset.right = 16;
    return inset;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row != self.selectIndex) {
        HBCategoryItemModel * categoryItemModel = self.categoryModel.sort_type[indexPath.row];
        [HBEvenManager event:@"Discovery_channel" evenLogType:(HBEvenLogType_Click) attributes:@{@"category_name":KEmptyStr(categoryItemModel.title)}];
        if ([self.delegate respondsToSelector:@selector(categoryHeadViewDidSelectItemAtIndexPath:)]) {
            [self.delegate categoryHeadViewDidSelectItemAtIndexPath:indexPath];
        }
    }
    self.selectIndex = indexPath.row;
    [self.listView reloadData];
    
    
}
@end
